<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>销售分析 - 企业管理系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2/dist/echarts.min.js"></script>
    <style>
        :root {
            --primary-color: #3498db;
            --secondary-color: #2c3e50;
            --success-color: #2ecc71;
            --danger-color: #e74c3c;
            --warning-color: #f39c12;
            --info-color: #9b59b6;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            padding: 20px;
        }

        .analysis-container {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            padding: 20px;
            margin-bottom: 20px;
        }

        .page-title {
            font-size: 22px;
            margin-bottom: 20px;
            color: var(--secondary-color);
            display: flex;
            align-items: center;
        }

        .page-title i {
            margin-right: 10px;
            color: var(--primary-color);
        }

        .filter-bar {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 6px;
            flex-wrap: wrap;
            gap: 15px;
        }

        .filter-group {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 10px;
        }

        .filter-group label {
            margin-right: 5px;
            font-weight: 500;
            white-space: nowrap;
        }

        select, input {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: white;
            min-width: 120px;
        }

        .btn {
            padding: 8px 16px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }

        .btn:hover {
            opacity: 0.9;
            transform: translateY(-1px);
        }

        .btn-success {
            background: var(--success-color);
        }

        .btn-warning {
            background: var(--warning-color);
        }

        .btn-danger {
            background: var(--danger-color);
        }

        .chart-container {
            height: 400px;
            margin-bottom: 30px;
            position: relative;
            border: 1px solid #eee;
            border-radius: 6px;
            padding: 15px;
        }

        .chart-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .chart-title {
            font-size: 16px;
            font-weight: 600;
            color: var(--secondary-color);
        }

        .chart-actions {
            display: flex;
            gap: 10px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-left: 4px solid var(--primary-color);
            transition: transform 0.3s;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-card h3 {
            font-size: 14px;
            color: #7f8c8d;
            margin-bottom: 10px;
        }

        .stat-card .value {
            font-size: 24px;
            font-weight: 600;
            color: var(--secondary-color);
            margin-bottom: 5px;
        }

        .stat-card .change {
            font-size: 12px;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .change.up {
            color: var(--success-color);
        }

        .change.down {
            color: var(--danger-color);
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .data-table th, .data-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        .data-table th {
            background: #f8f9fa;
            font-weight: 500;
            color: var(--secondary-color);
        }

        .data-table tr:hover {
            background: #f5f7fa;
        }

        .status-badge {
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 500;
        }

        .badge-success {
            background-color: #d4edda;
            color: #155724;
        }

        .badge-warning {
            background-color: #fff3cd;
            color: #856404;
        }

        .badge-danger {
            background-color: #f8d7da;
            color: #721c24;
        }

        .badge-info {
            background-color: #d1ecf1;
            color: #0c5460;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 5px;
        }

        .pagination a {
            padding: 8px 12px;
            border: 1px solid #ddd;
            color: var(--primary-color);
            text-decoration: none;
            border-radius: 4px;
        }

        .pagination a.active {
            background-color: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        .pagination a:hover:not(.active) {
            background-color: #f1f1f1;
        }

        .tabs {
            display: flex;
            border-bottom: 1px solid #ddd;
            margin-bottom: 20px;
        }

        .tab {
            padding: 10px 20px;
            cursor: pointer;
            border-bottom: 2px solid transparent;
            font-weight: 500;
        }

        .tab.active {
            border-bottom-color: var(--primary-color);
            color: var(--primary-color);
        }

        @media (max-width: 768px) {
            .filter-bar {
                flex-direction: column;
            }

            .filter-group {
                width: 100%;
            }

            select, input {
                width: 100%;
                margin-bottom: 10px;
            }

            .stats-grid {
                grid-template-columns: 1fr 1fr;
            }
        }
    </style>
</head>
<body>
<div class="analysis-container">
    <h1 class="page-title"><i class="bi bi-graph-up-arrow"></i>销售分析</h1>

    <div class="filter-bar">
        <div class="filter-group">
            <label><i class="bi bi-calendar"></i> 时间范围:</label>
            <select id="timeRange">
                <option>最近7天</option>
                <option>最近30天</option>
                <option>最近90天</option>
                <option selected>本月</option>
                <option>本季度</option>
                <option>本年</option>
                <option>自定义</option>
            </select>

            <div id="customDateRange" style="display:none;">
                <input type="date" id="startDate">
                <span>至</span>
                <input type="date" id="endDate">
            </div>
        </div>

        <div class="filter-group">
            <label><i class="bi bi-tags"></i> 产品类别:</label>
            <select id="productCategory">
                <option>全部类别</option>
                <option>电子产品</option>
                <option>家居用品</option>
                <option>服装</option>
                <option>食品</option>
            </select>

            <label><i class="bi bi-geo-alt"></i> 地区:</label>
            <select id="region">
                <option>全部地区</option>
                <option>华北</option>
                <option>华东</option>
                <option>华南</option>
                <option>西部</option>
            </select>

            <button class="btn" id="filterBtn"><i class="bi bi-funnel"></i> 筛选</button>
            <button class="btn btn-success" id="exportBtn"><i class="bi bi-download"></i> 导出</button>
        </div>
    </div>

    <div class="tabs">
        <div class="tab active">概览</div>
        <div class="tab">详细数据</div>
        <div class="tab">产品分析</div>
        <div class="tab">客户分析</div>
    </div>

    <div class="stats-grid">
        <div class="stat-card">
            <h3>总销售额</h3>
            <div class="value">¥1,245,678</div>
            <div class="change up"><i class="bi bi-arrow-up"></i> 12.5% 同比</div>
        </div>

        <div class="stat-card">
            <h3>订单数量</h3>
            <div class="value">3,456</div>
            <div class="change up"><i class="bi bi-arrow-up"></i> 8.2% 同比</div>
        </div>

        <div class="stat-card">
            <h3>客单价</h3>
            <div class="value">¥360.5</div>
            <div class="change up"><i class="bi bi-arrow-up"></i> 4.1% 同比</div>
        </div>

        <div class="stat-card">
            <h3>退货率</h3>
            <div class="value">2.8%</div>
            <div class="change down"><i class="bi bi-arrow-down"></i> 0.5% 同比</div>
        </div>
    </div>

    <div class="chart-container">
        <div class="chart-header">
            <div class="chart-title"><i class="bi bi-bar-chart-line"></i> 销售趋势</div>
            <div class="chart-actions">
                <button class="btn btn-success btn-sm" id="dailyBtn">日</button>
                <button class="btn btn-sm" id="weeklyBtn">周</button>
                <button class="btn btn-sm" id="monthlyBtn">月</button>
            </div>
        </div>
        <canvas id="salesTrendChart"></canvas>
    </div>

    <div class="chart-container">
        <div class="chart-header">
            <div class="chart-title"><i class="bi bi-pie-chart"></i> 产品销售占比</div>
            <div class="chart-actions">
                <button class="btn btn-sm" id="byRevenueBtn">按销售额</button>
                <button class="btn btn-sm" id="byQuantityBtn">按销售量</button>
            </div>
        </div>
        <div id="productPieChart" style="width:100%; height:350px;"></div>
    </div>

    <div class="chart-container">
        <div class="chart-header">
            <div class="chart-title"><i class="bi bi-people"></i> 客户购买分析</div>
            <div class="chart-actions">
                <button class="btn btn-sm" id="newCustomerBtn">新客户</button>
                <button class="btn btn-sm" id="repeatCustomerBtn">回头客</button>
            </div>
        </div>
        <canvas id="customerChart"></canvas>
    </div>

    <h2><i class="bi bi-table"></i> 销售明细</h2>
    <table class="data-table">
        <thead>
        <tr>
            <th>日期</th>
            <th>订单号</th>
            <th>客户</th>
            <th>产品</th>
            <th>数量</th>
            <th>金额</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>2023-06-15</td>
            <td>ORD20230615001</td>
            <td>张三</td>
            <td>智能手表</td>
            <td>2</td>
            <td>¥1,598.00</td>
            <td><span class="status-badge badge-success">已完成</span></td>
            <td><button class="btn btn-sm" style="padding: 3px 8px; font-size:12px;"><i class="bi bi-eye"></i> 查看</button></td>
        </tr>
        <tr>
            <td>2023-06-14</td>
            <td>ORD20230614005</td>
            <td>李四</td>
            <td>无线耳机</td>
            <td>1</td>
            <td>¥299.00</td>
            <td><span class="status-badge badge-success">已完成</span></td>
            <td><button class="btn btn-sm" style="padding: 3px 8px; font-size:12px;"><i class="bi bi-eye"></i> 查看</button></td>
        </tr>
        <tr>
            <td>2023-06-14</td>
            <td>ORD20230614003</td>
            <td>王五</td>
            <td>智能音箱</td>
            <td>3</td>
            <td>¥897.00</td>
            <td><span class="status-badge badge-warning">配送中</span></td>
            <td><button class="btn btn-sm" style="padding: 3px 8px; font-size:12px;"><i class="bi bi-eye"></i> 查看</button></td>
        </tr>
        <tr>
            <td>2023-06-13</td>
            <td>ORD20230613012</td>
            <td>赵六</td>
            <td>平板电脑</td>
            <td>1</td>
            <td>¥2,499.00</td>
            <td><span class="status-badge badge-danger">已退款</span></td>
            <td><button class="btn btn-sm" style="padding: 3px 8px; font-size:12px;"><i class="bi bi-eye"></i> 查看</button></td>
        </tr>
        <tr>
            <td>2023-06-12</td>
            <td>ORD20230612008</td>
            <td>钱七</td>
            <td>智能手机</td>
            <td>1</td>
            <td>¥3,299.00</td>
            <td><span class="status-badge badge-success">已完成</span></td>
            <td><button class="btn btn-sm" style="padding: 3px 8px; font-size:12px;"><i class="bi bi-eye"></i> 查看</button></td>
        </tr>
        <tr>
            <td>2023-06-11</td>
            <td>ORD20230611015</td>
            <td>孙八</td>
            <td>笔记本电脑</td>
            <td>1</td>
            <td>¥5,999.00</td>
            <td><span class="status-badge badge-info">待发货</span></td>
            <td><button class="btn btn-sm" style="padding: 3px 8px; font-size:12px;"><i class="bi bi-eye"></i> 查看</button></td>
        </tr>
        <tr>
            <td>2023-06-10</td>
            <td>ORD20230610022</td>
            <td>周九</td>
            <td>智能电视</td>
            <td>1</td>
            <td>¥4,299.00</td>
            <td><span class="status-badge badge-success">已完成</span></td>
            <td><button class="btn btn-sm" style="padding: 3px 8px; font-size:12px;"><i class="bi bi-eye"></i> 查看</button></td>
        </tr>
        </tbody>
    </table>

    <div class="pagination">
        <a href="#">&laquo;</a>
        <a href="#" class="active">1</a>
        <a href="#">2</a>
        <a href="#">3</a>
        <a href="#">4</a>
        <a href="#">&raquo;</a>
    </div>
</div>

<script>
    // 时间范围选择切换
    document.getElementById('timeRange').addEventListener('change', function() {
        const customDateRange = document.getElementById('customDateRange');
        if (this.value === '自定义') {
            customDateRange.style.display = 'flex';
            customDateRange.style.alignItems = 'center';
            customDateRange.style.gap = '10px';
        } else {
            customDateRange.style.display = 'none';
        }
    });

    // 初始化销售趋势图表
    const salesTrendCtx = document.getElementById('salesTrendChart').getContext('2d');
    const salesTrendChart = new Chart(salesTrendCtx, {
        type: 'line',
        data: {
            labels: ['1月', '2月', '3月', '4月', '5月', '6月'],
            datasets: [
                {
                    label: '销售额',
                    data: [1250000, 1450000, 1320000, 1580000, 1720000, 1850000],
                    borderColor: '#3498db',
                    backgroundColor: 'rgba(52, 152, 219, 0.1)',
                    borderWidth: 2,
                    tension: 0.3,
                    fill: true
                },
                {
                    label: '订单数',
                    data: [2850, 3120, 2980, 3250, 3420, 3650],
                    borderColor: '#2ecc71',
                    backgroundColor: 'rgba(46, 204, 113, 0.1)',
                    borderWidth: 2,
                    tension: 0.3,
                    fill: false,
                    yAxisID: 'y1'
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    position: 'top',
                },
                tooltip: {
                    mode: 'index',
                    intersect: false,
                    callbacks: {
                        label: function(context) {
                            let label = context.dataset.label || '';
                            if (label) {
                                label += ': ';
                            }
                            if (context.datasetIndex === 0) {
                                label += '¥' + context.raw.toLocaleString();
                            } else {
                                label += context.raw.toLocaleString() + ' 单';
                            }
                            return label;
                        }
                    }
                }
            },
            scales: {
                y: {
                    type: 'linear',
                    display: true,
                    position: 'left',
                    title: {
                        display: true,
                        text: '销售额 (¥)'
                    },
                    ticks: {
                        callback: function(value) {
                            return '¥' + (value / 10000).toLocaleString() + '万';
                        }
                    }
                },
                y1: {
                    type: 'linear',
                    display: true,
                    position: 'right',
                    title: {
                        display: true,
                        text: '订单数'
                    },
                    grid: {
                        drawOnChartArea: false,
                    }
                }
            }
        }
    });

    // 初始化产品销售占比图表 (使用ECharts)
    const productPieChart = echarts.init(document.getElementById('productPieChart'));
    productPieChart.setOption({
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            right: 10,
            top: 'center',
            data: ['电子产品', '家居用品', '服装', '食品', '其他']
        },
        series: [
            {
                name: '销售额占比',
                type: 'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: '18',
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: [
                    { value: 1048000, name: '电子产品', itemStyle: { color: '#3498db' } },
                    { value: 735000, name: '家居用品', itemStyle: { color: '#2ecc71' } },
                    { value: 580000, name: '服装', itemStyle: { color: '#e74c3c' } },
                    { value: 484000, name: '食品', itemStyle: { color: '#f39c12' } },
                    { value: 300000, name: '其他', itemStyle: { color: '#9b59b6' } }
                ]
            }
        ]
    });

    // 初始化客户分析图表
    const customerCtx = document.getElementById('customerChart').getContext('2d');
    const customerChart = new Chart(customerCtx, {
        type: 'bar',
        data: {
            labels: ['1月', '2月', '3月', '4月', '5月', '6月'],
            datasets: [
                {
                    label: '新客户',
                    data: [120, 150, 130, 170, 190, 210],
                    backgroundColor: 'rgba(52, 152, 219, 0.7)',
                    borderColor: 'rgba(52, 152, 219, 1)',
                    borderWidth: 1
                },
                {
                    label: '回头客',
                    data: [180, 200, 220, 240, 260, 280],
                    backgroundColor: 'rgba(46, 204, 113, 0.7)',
                    borderColor: 'rgba(46, 204, 113, 1)',
                    borderWidth: 1
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    position: 'top',
                },
                tooltip: {
                    mode: 'index',
                    intersect: false
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    title: {
                        display: true,
                        text: '客户数量'
                    }
                }
            }
        }
    });

    // 标签切换
    document.querySelectorAll('.tab').forEach(tab => {
        tab.addEventListener('click', function() {
            document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
            this.classList.add('active');
            // 这里可以添加标签切换逻辑
        });
    });

    // 图表按钮切换
    document.getElementById('dailyBtn').addEventListener('click', function() {
        // 切换到日视图逻辑
        this.classList.add('btn-success');
        document.getElementById('weeklyBtn').classList.remove('btn-success');
        document.getElementById('monthlyBtn').classList.remove('btn-success');
    });

    document.getElementById('weeklyBtn').addEventListener('click', function() {
        // 切换到周视图逻辑
        this.classList.add('btn-success');
        document.getElementById('dailyBtn').classList.remove('btn-success');
        document.getElementById('monthlyBtn').classList.remove('btn-success');
    });

    document.getElementById('monthlyBtn').addEventListener('click', function() {
        // 切换到月视图逻辑
        this.classList.add('btn-success');
        document.getElementById('dailyBtn').classList.remove('btn-success');
        document.getElementById('weeklyBtn').classList.remove('btn-success');
    });

    // 产品占比切换
    document.getElementById('byRevenueBtn').addEventListener('click', function() {
        // 按销售额显示
        this.classList.add('btn-success');
        document.getElementById('byQuantityBtn').classList.remove('btn-success');
        // 更新图表数据
    });

    document.getElementById('byQuantityBtn').addEventListener('click', function() {
        // 按销售量显示
        this.classList.add('btn-success');
        document.getElementById('byRevenueBtn').classList.remove('btn-success');
        // 更新图表数据
    });

    // 客户分析切换
    document.getElementById('newCustomerBtn').addEventListener('click', function() {
        // 显示新客户数据
        this.classList.add('btn-success');
        document.getElementById('repeatCustomerBtn').classList.remove('btn-success');
        // 更新图表数据
    });

    document.getElementById('repeatCustomerBtn').addEventListener('click', function() {
        // 显示回头客数据
        this.classList.add('btn-success');
        document.getElementById('newCustomerBtn').classList.remove('btn-success');
        // 更新图表数据
    });

    // 筛选按钮
    document.getElementById('filterBtn').addEventListener('click', function() {
        // 执行筛选逻辑
        alert('筛选条件已应用');
    });

    // 导出按钮
    document.getElementById('exportBtn').addEventListener('click', function() {
        // 执行导出逻辑
        alert('数据导出中...');
    });

    // 响应式调整
    window.addEventListener('resize', function() {
        salesTrendChart.resize();
        productPieChart.resize();
        customerChart.resize();
    });
</script>
</body>
</html>